package com.neusoft.controller;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.portlet.bind.annotation.ResourceMapping;

import com.neusoft.entity.Dept;
import com.neusoft.entity.Emp;
import com.neusoft.service.EmpService;

@Controller
@RequestMapping("/Emp")
public class EmpController {

	@Autowired
	private EmpService empService=null;

	public EmpService getEmpService() {
		return empService;
	}

	public void setEmpService(EmpService empService) {
		this.empService = empService;
	}
	
	@RequestMapping("/1.do")
	public String getEmpByDeptno(HttpServletRequest request){
		String deptnoStr=request.getParameter("deptno");
		int deptno=Integer.parseInt(deptnoStr);
		List<Emp> list = empService.getEmpbyDeptno(deptno);
		System.out.println(list);
		request.setAttribute("list", list);
		return "showEmpByDeptno";
	}
	
	@RequestMapping("/2.do")
	public @ResponseBody Emp getEmpByNo(@RequestParam (value="empno") int empno,
			HttpServletRequest request){
		Emp emp=empService.getEmpbyEmpno(empno);
		System.out.println(emp);
        return emp;
	}
	
	@RequestMapping("/3.do")
	public String addEmp(HttpServletRequest request) throws ParseException{
		
		String empnoStr=request.getParameter("empno");
		int empno=Integer.parseInt(empnoStr);
		
		String ename=request.getParameter("ename");
		
		String job=request.getParameter("job");
		
		String hireDateStr=request.getParameter("hireDate");
		java.text.SimpleDateFormat timeformat = new java.text.SimpleDateFormat(
                "yyyy-MM-dd");
		Date hireDate=timeformat.parse(hireDateStr);
		
		String salStr=request.getParameter("sal");
		double sal=Double.parseDouble(salStr);
		
		String deptnoStr=request.getParameter("deptno");
		int deptno=Integer.parseInt(deptnoStr);
		
		Emp emp=new Emp(empno,deptno,ename,sal,job,hireDate);
		empService.insertEmp(emp);
		return "insertEmp";
	}
	
	
	@RequestMapping("/4.do")
	public String updataDeptno(HttpServletRequest request){
		String empnoString=request.getParameter("empno");
		int empno=Integer.parseInt(empnoString);
		String deptnoString=request.getParameter("deptno");
		int deptno=Integer.parseInt(deptnoString);
		empService.moveEmp(empno, deptno);
		Emp emp=new Emp();
		emp.setEmpno(empno);
		emp.setDeptno(deptno);
		request.setAttribute("emp", emp);
		return "moveEmp";
	}
	
	@RequestMapping("/5.do")
	public String updataTotalSal(HttpServletRequest request){
		String deptnoString=request.getParameter("deptno");
		int deptno=Integer.parseInt(deptnoString);
		String totalSalString=request.getParameter("totalSal");
		double totalSal=Double.parseDouble(totalSalString);
		empService.updateTotalSal(deptno, totalSal);
		Dept dept=new Dept();
		dept.setDeptno(deptno);
		dept.setTotalSal(totalSal);
		request.setAttribute("dept", dept);
		return "updataTotalSal";
	}
}
